package com.ys.nms.module.business.controller.admin.devicesLog;

import com.baomidou.dynamic.datasource.annotation.Slave;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.devicesLog.vo.*;
import com.ys.nms.module.business.dal.dataobject.devicesLog.DevicesLogDO;
import com.ys.nms.module.business.service.devicesLog.DevicesLogService;

@Tag(name = "管理后台 - 设备日志")
@RestController
@RequestMapping("/business/devices-log")
@Validated
public class DevicesLogController {

    @Resource
    private DevicesLogService devicesLogService;

    @PostMapping("/create")
    @Operation(summary = "创建设备日志")
    public CommonResult<Integer> createDevicesLog(@Valid @RequestBody DevicesLogSaveReqVO createReqVO) {
        return success(devicesLogService.createDevicesLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备日志")
    public CommonResult<Boolean> updateDevicesLog(@Valid @RequestBody DevicesLogSaveReqVO updateReqVO) {
        devicesLogService.updateDevicesLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备日志")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDevicesLog(@RequestParam("id") Integer id) {
        devicesLogService.deleteDevicesLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备日志")
    public CommonResult<Boolean> deleteDevicesLogList(@RequestParam("ids") List<Integer> ids) {
        devicesLogService.deleteDevicesLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DevicesLogRespVO> getDevicesLog(@RequestParam("id") Integer id) {
        DevicesLogDO devicesLog = devicesLogService.getDevicesLog(id);
        return success(BeanUtils.toBean(devicesLog, DevicesLogRespVO.class));
    }

    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得设备日志分页")
    public CommonResult<PageResult<DevicesLogRespVO>> getDevicesLogPage(@Valid DevicesLogPageReqVO pageReqVO) {
        PageResult<DevicesLogDO> pageResult = devicesLogService.getDevicesLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DevicesLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备日志 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevicesLogExcel(@Valid DevicesLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DevicesLogDO> list = devicesLogService.getDevicesLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备日志.xls", "数据", DevicesLogRespVO.class,
                        BeanUtils.toBean(list, DevicesLogRespVO.class));
    }

}